/*global define:true Vida:true ko:true*/

define(['ajax'], function (ajax) {
    "use strict";

    var exports = {},
    Email, Sms, Network, ViewModel;

    Email = function () {
        var self = this;
        self.editting = ko.observable(false);

        self.mailHost = ko.observable().revertable();
        self.port = ko.observable().revertable();
        self.user = ko.observable().revertable();
        self.password = ko.observable().revertable();
        self.from = ko.observable().revertable();
        self.displayname = ko.observable().revertable();
        self.replyTo = ko.observable().revertable();
        self.ssl = ko.observable().revertable();
        self.tls = ko.observable().revertable();

        self.revert = function () {
            self.mailHost.revert();
            self.port.revert();
            self.user.revert();
            self.password.revert();
            self.from.revert();
            self.displayname.revert();
            self.replyTo.revert();
            self.ssl.revert();
            self.tls.revert();
        };

        self.commit = function () {
            self.mailHost.commit();
            self.port.commit();
            self.user.commit();
            self.password.commit();
            self.from.commit();
            self.displayname.commit();
            self.replyTo.commit();
            self.ssl.commit();
            self.tls.commit();
        };

        self.edit = function (configuration, evt) {
            evt.preventDefault();
            configuration.editting(true);
        };

        self.load = function () {
            ajax.get('emailconfig', { showSuccessMessage: false }).done(function (response) {
                self.mailHost(response.mailHost);
                self.port(response.port);
                self.user(response.user);
                self.password(response.password);
                self.from(response.from);
                self.displayname(response.displayName);
                self.replyTo(response.replyTo);
                self.ssl(response.ssl);
                self.tls(response.tls);
            });
        };

        self.update = function () {
            var from = {
                mailHost: self.mailHost(),
                port: self.port(),
                user: self.user(),
                password: self.password(),
                from: self.from(),
                displayName: self.displayname(),
                replyTo: self.replyTo(),
                ssl: self.ssl(),
                tls: self.tls(),
            };
            ajax.put('emailconfig', from, { showSuccessMessage: false })
                .fail(function (res) {
                    self.editting(true);
                })
                .always(function () {
                    self.editting(false);
                })
                .done(function (res) {                    
                    self.commit();
                });
        };

        self.cancel = function (configuration, evt) {
            evt.preventDefault();
            configuration.revert();
            configuration.editting(false);
        };

    };

    Sms = function () {
        var self = this;
        self.editting = ko.observable(false);

        self.serviceUrl = ko.observable().revertable();
        self.senderName = ko.observable().revertable();

        self.revert = function () {
            self.serviceUrl.revert();
            self.senderName.revert();
        };

        self.commit = function () {
            self.serviceUrl.commit();
            self.senderName.commit();
        };

        self.edit = function (configuration, evt) {
            evt.preventDefault();
            configuration.editting(true);
        };

        self.load = function () {
            ajax.get('smsconfig', { showSuccessMessage: false }).done(function (response) {
                self.serviceUrl(response.serviceUrl);
                self.senderName(response.senderName);
            });
        };

        self.update = function () {
            var from = {
                serviceUrl: self.serviceUrl(),
                senderName: self.senderName(),
            };
            ajax.put('smsconfig', from, { showSuccessMessage: false })
                .fail(function () {
                    self.revert();
                })
                .always(function () {
                    self.editting(false);
                })
                .done(function () {
                    self.commit();
                });
        };

        self.cancel = function (configuration, evt) {
            evt.preventDefault();
            configuration.revert();
            configuration.editting(false);
        };
    };


    Network = function () {
        var self = this;
        self.editting = ko.observable(false);

        self.proxyHost = ko.observable().revertable();
        self.proxyPort = ko.observable().revertable();
        self.userName = ko.observable().revertable();
        self.password = ko.observable().revertable();
        self.useProxy = ko.observable().revertable();
        self.useCredential = ko.observable().revertable();

        self.revert = function () {
            self.proxyHost.revert();
            self.proxyPort.revert();
            self.userName.revert();
            self.password.revert();
            self.useProxy.revert();
            self.useCredential.revert();
        };

        self.commit = function () {
            self.proxyHost.commit();
            self.proxyPort.commit();
            self.userName.commit();
            self.password.commit();
            self.useProxy.commit();
            self.useCredential.commit();
        };

        self.edit = function (configuration, evt) {
            evt.preventDefault();
            configuration.editting(true);
        };

        self.load = function () {
            ajax.get('networkconfig', { showSuccessMessage: false }).done(function (response) {
                self.proxyHost(response.proxyHost);
                self.proxyPort(response.proxyPort);
                self.userName(response.userName);
                self.password(response.password);
                self.useProxy(response.useProxy);
                self.useCredential(response.useCredential);
            });
        };

        self.update = function () {
            var from = {
                proxyHost: self.proxyHost(),
                proxyPort: self.proxyPort(),
                userName: self.userName(),
                password: self.password(),
                useProxy: self.useProxy(),
                useCredential: self.useCredential(),
            };
            ajax.put('networkconfig', from, { showSuccessMessage: false })
                .fail(function () {
                    self.revert();
                })
                .always(function () {
                    self.editting(false);
                })
                .done(function () {
                    self.commit();
                });
        };

        self.cancel = function (configuration, evt) {
            evt.preventDefault();
            configuration.revert();
            configuration.editting(false);
        };

    };

    ViewModel = function () {
        var self = this;

        self.emailConfiguration = ko.observable(true);
        self.smsConfiguration = ko.observable(false);
        self.networkConfiguration = ko.observable(false);
        self.downloadAttandanceReport = function () {
            $.ajax({
                type: 'POST',
                url: '/coreConfiguration/download',
                contentType: 'application/json',
            }).done(function (response) {
                return confirm(response);
            });
        };

        self.email = ko.observable(new Email());
        self.sms = ko.observable(new Sms());
        self.network = ko.observable(new Network());
        self.email().load();

        self.emailConfig = function () {
            self.emailConfiguration(true);
            self.smsConfiguration(false);
            self.networkConfiguration(false);
            self.email().load();
        };

        self.smsConfig = function () {
            self.emailConfiguration(false);
            self.smsConfiguration(true);
            self.networkConfiguration(false);
            self.sms().load();
        };

        self.networkConfig = function () {
            self.emailConfiguration(false);
            self.smsConfiguration(false);
            self.networkConfiguration(true);
            self.network().load();
        };
    };

    exports.exec = function (sectionId, model) {

        var viewModel = new ViewModel(model);
        ko.applyBindings(viewModel, document.getElementById(sectionId));
    };

    return exports;
});